package com.main.by.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.main.by.dao.AdminDao;
import com.main.by.dao.GeneralDao;
import com.main.by.entity.bo.AdminLoginBO;
import com.main.by.entity.vo.AdminLoginVO;
import com.main.by.util.StringUtil;

@Repository
public class AdminDaoImpl implements AdminDao {
	
	Logger logger = Logger.getLogger(this.getClass());
	
	@Autowired
	private GeneralDao generalDao;

	@Override
	public AdminLoginVO getAdminByUserName(String adminName) throws Exception {
		if(StringUtil.isEmpty(adminName)){
			return null;
		}
		return generalDao.getEntity(AdminLoginVO.class, " select * from renren_admin where adminName = ? ", new Object[]{adminName});
	}

	@Override
	public boolean updateSysUser(AdminLoginBO user, String... fileds) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean addAdmin(AdminLoginVO admin) throws Exception {
		boolean successful = generalDao.saveEntity("insert into renren_admin(adminName,adminPass,adminPhone) values(?,MD5(?),?)", 
				new Object[]{admin.getAdminName(),admin.getAdminPass(),admin.getAdminPhone()});
		return successful;
	}
	

	//查询所有的管理员
	@SuppressWarnings("unchecked")
	@Override
	public List<AdminLoginVO> findAdmin() throws Exception {
		return (List<AdminLoginVO>) generalDao.findAllEntity(AdminLoginVO.class, "select * from renren_admin");
	}

	@Override
	public AdminLoginVO findAdminById(int adminId) throws Exception {
		return generalDao.getEntity(AdminLoginVO.class,new Object[]{adminId});
	}

	@Override
	public boolean deleteAdminById(int adminId) throws Exception {
		return generalDao.updateEntity("delete from renren_admin where adminId=? ",new Object[]{adminId});
	}

	@Override
	public boolean updateAdminById(AdminLoginVO admin) throws Exception {
		boolean successful = generalDao.updateEntity("update renren_admin set adminName=?,adminPass=?,adminPhone=? where adminId=?", 
				new Object[]{admin.getAdminName(),admin.getAdminPass(),admin.getAdminPhone(),admin.getAdminId()});
		return successful;
	}
	
	


}
